<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>反射与内省机制 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/29.088e0d73.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础高级</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0c7696/" class="sidebar-link">集合框架</a></li><li><a href="/pages/19e70b/" class="sidebar-link">异常机制</a></li><li><a href="/pages/a53030/" class="sidebar-link">多线程</a></li><li><a href="/pages/16b819/" class="sidebar-link">IO</a></li><li><a href="/pages/258730/" class="sidebar-link">单元测试与配置文件</a></li><li><a href="/pages/1fdc1c/" aria-current="page" class="active sidebar-link">反射与内省机制</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/1fdc1c/#一、学习内容与目标" class="sidebar-link">一、学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/1fdc1c/#二、反射" class="sidebar-link">二、反射</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_2-1-未用反射的问题-理解" class="sidebar-link">2.1 未用反射的问题(理解)</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_2-2-反射" class="sidebar-link">2.2 反射</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-2-1-字节码对象-理解" class="sidebar-link">2.2.1 字节码对象(理解)</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-2-2-获取字节码对象-掌握" class="sidebar-link">2.2.2 获取字节码对象(掌握)</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_2-3-获取构造器" class="sidebar-link">2.3 获取构造器</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-3-1-正常调用方法的流程" class="sidebar-link">2.3.1 正常调用方法的流程</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-3-2-反射操作构造器" class="sidebar-link">2.3.2 反射操作构造器</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_2-4-获取操作方法" class="sidebar-link">2.4 获取操作方法</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-4-1-反射获取方法" class="sidebar-link">2.4.1 反射获取方法</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-4-2-调用方法" class="sidebar-link">2.4.2 调用方法</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_2-5-操作字段-了解" class="sidebar-link">2.5 操作字段(了解)</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-5-1-获取字段" class="sidebar-link">2.5.1 获取字段</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_2-5-2-操作字段" class="sidebar-link">2.5.2 操作字段</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/1fdc1c/#三、javabean" class="sidebar-link">三、JavaBean</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_3-1-javabean-介绍" class="sidebar-link">3.1 JavaBean 介绍</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_3-2-lombok-使用" class="sidebar-link">3.2 LomBok 使用</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_3-2-1-lombok-插件安装" class="sidebar-link">3.2.1 Lombok 插件安装</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_3-2-2-lombok-工具包导入" class="sidebar-link">3.2.2 Lombok 工具包导入</a></li><li class="sidebar-sub-header level4"><a href="/pages/1fdc1c/#_3-2-3-lombok-的使用-掌握" class="sidebar-link">3.2.3 Lombok 的使用(掌握)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/1fdc1c/#四、内省" class="sidebar-link">四、内省</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-1-内省介绍" class="sidebar-link">4.1 内省介绍</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-2-内省的作用" class="sidebar-link">4.2 内省的作用:</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-3-内省常用的api" class="sidebar-link">4.3 内省常用的API</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-4-实战" class="sidebar-link">4.4 实战</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-5-javabean-和-map-之间的转化" class="sidebar-link">4.5 JavaBean 和 Map 之间的转化</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_4-6-小结" class="sidebar-link">4.6 小结</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/1fdc1c/#五、小结与作业" class="sidebar-link">五、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_5-1-小结" class="sidebar-link">5.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/1fdc1c/#_5-2-作业" class="sidebar-link">5.2 作业</a></li></ul></li></ul></li><li><a href="/pages/80d6e3/" class="sidebar-link">Lambda表达式</a></li><li><a href="/pages/a53dcc/" class="sidebar-link">Stream流操作</a></li><li><a href="/pages/42948c/" class="sidebar-link">注解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E9%AB%98%E7%BA%A7" title="分类" data-v-06225672>基础高级</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">反射与内省机制<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="反射与内省机制"><a href="#反射与内省机制" class="header-anchor">#</a> 反射与内省机制</h1> <h2 id="一、学习内容与目标"><a href="#一、学习内容与目标" class="header-anchor">#</a> 一、学习内容与目标</h2> <ul><li>理解为什么要使用反射</li> <li>理解什么是字节码对象</li> <li>熟练掌握获取字节码对象的三种方法</li> <li>熟练掌握反射操作构造器/方法</li> <li>理解掌握 JavaBean 规范</li> <li>了解 Lombok 插件的安装</li> <li>掌握 Lombok 工具的使用</li> <li>掌握内省的基本操作(属性的操作)</li></ul> <h2 id="二、反射"><a href="#二、反射" class="header-anchor">#</a> 二、反射</h2> <h3 id="_2-1-未用反射的问题-理解"><a href="#_2-1-未用反射的问题-理解" class="header-anchor">#</a> 2.1 未用反射的问题(理解)</h3> <blockquote><p><strong>需求</strong>: 书写一个牛逼的工具方法,获取打印传入的 JavaBean 中的数据.</p></blockquote> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 测试代码</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUtil</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;小普&quot;</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用工具方法打印对象</span>
    <span class="token class-name">JavaBeanUtil</span><span class="token punctuation">.</span><span class="token function">printJavaBean</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// JavaBeanUtil.printJavaBean(person);</span>
<span class="token punctuation">}</span>

<span class="token comment">// 工具类</span>
<span class="token keyword">class</span> <span class="token class-name">JavaBeanUtil</span><span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">printJavaBean</span><span class="token punctuation">(</span><span class="token class-name">Object</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// 如果知道调用者传入的真实类型是User,代码如下</span>
        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">User</span><span class="token punctuation">)</span>obj<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><strong>上面代码存在的问题</strong>:</p> <ol><li>printJavaBean 方法中要获取传入的 User 对象的数据,就必须强转.</li> <li>咱们写的是牛逼的工具方法,<strong>在不知调用者带了什么对象时,无法强转</strong>,强转则有风险(类型转化异常).</li> <li>在 printJavaBean 方法中已经拿到对象了,却<strong>得不到对象的真实类型</strong>.</li></ol> <p><strong>解决方案</strong>: 使用反射 (可以通过一个对象,获取到它的真实类型以及调用该对象中的方法).</p> <h3 id="_2-2-反射"><a href="#_2-2-反射" class="header-anchor">#</a> 2.2 反射</h3> <p>在程序运行过程中，通过<strong>字节码对象</strong>，去获取到类中的成员信息（构造器，方法，字段）,这就称为反射.</p> <p>目的是通过程序自动获取构造器来创建对象.获取到方法并调用等等.</p> <h4 id="_2-2-1-字节码对象-理解"><a href="#_2-2-1-字节码对象-理解" class="header-anchor">#</a> 2.2.1 字节码对象(理解)</h4> <p>在 Java 中,万物皆对象.我们可以通过多个事物,发现他们的共性,来抽象成一个类,类就是对象的模板,而一个个的个体,就是对象. 比如人类和学生.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/Class.jpg" alt=""></p> <p>字节码也是真实存在的文件,每个字节码都是一个个例,而 JVM 要来存放这些字节码就需要抽象成模板,再通过模板来创建对象,存放每份字节码的信息.当要使用某份字节码时(例如要创建Person对象),就从 JVM 中调出存了Person.class 内容的Class 对象,然后拿去创建 Person 对象.</p> <p><strong>JDK 中定义好的 Class 类</strong>: java.lang.Class</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426002042944.png" alt="image-20200426002042944"></p> <p>该类中有大量的 get 开头的方法.表示可以使用字节码对象来获取信息.<strong>所以当我们拿到了字节码对象，就可以直接操作当前字节码中的构造器,方法,字段.</strong></p> <h4 id="_2-2-2-获取字节码对象-掌握"><a href="#_2-2-2-获取字节码对象-掌握" class="header-anchor">#</a> 2.2.2 获取字节码对象(掌握)</h4> <p>通过 API ,我们得知 Class ,没有公共的构造器,其原因是 Class 对象是在加载类时由 Java 虚拟机自动构造的。</p> <p><strong>获取字节码的方式</strong>:</p> <ol><li>通过 Class 类的 forName() 方法来获取字节码对象.
<ul><li>Class.forName(String classsName) : 通过<strong>类的全限定名</strong>获取字节码对象</li> <li>全限定名: 包名.类型</li> <li>Class.forName(&quot;java.lang.String&quot;);   // JVM 中存在则返回,不存在则加载</li></ul></li> <li>通过对象的 getClass() 方法来获取字节码对象
<ul><li>new User().getClass();  使用的是父类 Object 中的 getClass() 方法</li></ul></li> <li>通过类型(基本类型)的 class 字段来获取字节码对象
<ul><li>int.class</li></ul></li></ol> <p>上面三种方式应用都比较多,尤其是第一种方式,通过 Class.forName,大量的在框架中使用.</p> <p><strong>实例</strong>:</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426010732744.png" alt="image-20200426010732744"></p> <p><strong>思考:</strong></p> <ol><li>三种方式获取到的字节码是同一个吗?</li> <li>int 类型和 int[] 它们的字节码是同一个吗?</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testGetClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1 通过类的全限定名 Class.forName();</span>
    <span class="token class-name">Class</span> clz1 <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode._01_reflect.Person&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 通过对象的getClass() 方法</span>
    <span class="token class-name">Person</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Class</span> clz2 <span class="token operator">=</span> p<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 3 通过class 字段去获取</span>
    <span class="token class-name">Class</span> clz3 <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span>
    <span class="token comment">// 字节码只会加载一次,所有不管用的哪种方式去获取字节码,都是同一个</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>clz1 <span class="token operator">==</span> clz2<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//true</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>clz2 <span class="token operator">==</span> clz3<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//true</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>clz1 <span class="token operator">==</span> clz3<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//true</span>

    <span class="token comment">// int 类型和int数据类型不是同一个</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="_2-3-获取构造器"><a href="#_2-3-获取构造器" class="header-anchor">#</a> 2.3 获取构造器</h3> <h4 id="_2-3-1-正常调用方法的流程"><a href="#_2-3-1-正常调用方法的流程" class="header-anchor">#</a> 2.3.1 正常调用方法的流程</h4> <ol><li>非 static 修饰的方法: 创建对象,调用方法</li> <li>static 修饰的方法 : 类名调用方法</li></ol> <blockquote><p>使用反射其目的无外乎就是使用程序动态操作类的成员,比如方法,而要操作方法首先得有对象,而对象是通过构造器来创建的,所以需要先获取构造器.</p></blockquote> <h4 id="_2-3-2-反射操作构造器"><a href="#_2-3-2-反射操作构造器" class="header-anchor">#</a> 2.3.2 反射操作构造器</h4> <p><strong>使用反射获取构造器</strong> :</p> <ul><li><p>获取所有的构造器
public Constructor[] getConstructors(): 获取所有的 public 修饰的构造器
public Constructor[] getDeclaredConstructors(): 获取所有的构造器(包括非public)</p></li> <li><p>获取指定的构造器
public Constructor getConstructor(Class... parameterTypes)
public Constructor  getDeclaredConstructor(Class... parameterTypes):
parameterTypes : 参数的类型(构造方法的参数列表的类型).</p> <p>​        <strong>注意</strong>: 找构造器/方法,传递的是参数的类型.</p> <p><strong>结论</strong> : 带着 s 表示获取多个.带着 Declared 表示忽略权限,包括私有的也可以获取到.</p></li></ul> <p>实战:</p> <blockquote><p>1 获取所有 public 构造器
2 获取所有构造器,包括 private
3 获取无参构造器
4 获取带参构造器
5 获取指定 private 构造器</p></blockquote> <p>代码:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGetConstructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NoSuchMethodException</span> <span class="token punctuation">{</span>
    <span class="token comment">// 获取字节码对象</span>
    <span class="token class-name">Class</span> clz <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span>
    <span class="token comment">// 1 获取所有 public 构造器</span>
    <span class="token class-name">Constructor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cons1 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getConstructors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Constructor</span> con <span class="token operator">:</span> cons1<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>con<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;--------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 获取所有构造器,包括 private</span>
    <span class="token class-name">Constructor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cons2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredConstructors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Constructor</span> con <span class="token operator">:</span> cons2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>con<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 3 获取无参构造器</span>
    <span class="token class-name">Constructor</span> con1 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>con1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 4 获取带参构造器</span>
    <span class="token class-name">Constructor</span> con2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span><span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>con2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 5 获取指定 private 构造器</span>
    <span class="token class-name">Constructor</span> con3 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredConstructor</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>con3<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>常见错误: 参数不匹配,报错.找不到指定的构造器</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426080849307.png" alt="image-20200426080849307"></p> <p><strong>调用构造器创建对象</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> initargs<span class="token punctuation">)</span>
<span class="token comment">// initargs: 调用该构造器传递的实际参数.参数列表一定要匹配(类型,个数,顺序).</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>实战:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testCreateObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 获取字节码对象</span>
    <span class="token class-name">Class</span> clz <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode._04_reflect.Person&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取带参数构造器,参数为参数类型</span>
    <span class="token class-name">Constructor</span> con1 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span><span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//调用构造器</span>
    <span class="token class-name">Object</span> obj <span class="token operator">=</span> con1<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">,</span> <span class="token string">&quot;小狼&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取带有参数的 private 构造器</span>
    <span class="token class-name">Constructor</span> con2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredConstructor</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用私有构造器,必须先设置为可访问</span>
    con2<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Object</span> obj2 <span class="token operator">=</span> con2<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token string">&quot;小码&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>注意</strong>: 不能直接访问没有权限(非public)的成员,如果想要使用反射去操作非public的成员.必须设置一个可以访问的标记.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426081810803.png" alt="image-20200426081810803"></p> <blockquote><p>public void setAccessible(boolean flag): 传递一个true,表示可以访问,表示不管权限.</p></blockquote> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426081857403.png" alt="image-20200426081857403"></p> <p>从 API 中我们可以发现,Constructor,Field,Method是 AccessibleObject 的子类,因为这三种成员都是可以被访问private 修饰符修饰的.</p> <p><strong>代码</strong>:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testCreateObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 获取带有参数的 private 构造器</span>
    <span class="token class-name">Constructor</span> con2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredConstructor</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用私有构造器,必须先设置为可访问</span>
    con2<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Object</span> obj2 <span class="token operator">=</span> con2<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token string">&quot;小码&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>创建对象的快捷方式</strong>: 调用公共无参构造器</p> <p>一般来说,大多数类都有公共的无参数的构造器.使用反射创建对象,需要 1.找到公共的无参数的构造器, 2. 调用构造器的newInstance.</p> <p>SUN公司为了方便我们调用<strong>公共的无参数</strong>的构造器来创建对象,提供了一个方便的方法. <strong>Class对象.newInstance()</strong>;</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426082319103.png" alt="image-20200426082319103"></p> <p><strong>经验</strong> :
只要看到传入全限定名,基本上都是要使用反射,通过全限定名来获取字节码对象.</p> <p>只要看到无指定构造器但是能创建对象,基本上都是要通过Class对象的 newInstance 去创建对象.</p> <h3 id="_2-4-获取操作方法"><a href="#_2-4-获取操作方法" class="header-anchor">#</a> 2.4 获取操作方法</h3> <p>在反射中,使用 Method 类来描述方法这一类事物.</p> <h4 id="_2-4-1-反射获取方法"><a href="#_2-4-1-反射获取方法" class="header-anchor">#</a> 2.4.1 反射获取方法</h4> <p><strong>获取所有方法</strong>:</p> <ul><li>public Method[] getMethods(): 可以获取到所有的公共的方法,包括继承的.+</li> <li>public Method[] getDeclaredMethods():获取到本类中所有的方法,包括非public的,不包括继承的.</li></ul> <p>​</p> <p><strong>获取指定的方法</strong>:</p> <ul><li>public Method getMethod(String name, Class... parameterTypes):</li> <li>public Method getDeclaredMethod(String name, Class... parameterTypes):
name: 方法名
parameterTypes: 当前方法的参数列表的类型.</li> <li>注意,要找到某一个指定的方法,必须要使用方法签名才能定位到.而方法签名=方法名+参数列表,还记得我们获取构造器的经验吗?带着s表示获取多个,带着declared表示忽略访问权限.</li></ul> <p>实战</p> <blockquote><p>1 获取所有 public 方法,包括父类的
2 获取所有方法,包括 private 不包括父类的
3 获取指定参数的 public 的方法,包括父类的
4 获取指定参数的 private 方法,不包括父类的</p></blockquote> <p>代码</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testGetMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1 获取字节码对象</span>
    <span class="token class-name">Class</span> clz <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode._01_reflect.Person&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 获取构造器来创建对象</span>
    <span class="token comment">// 3 获取方法</span>
    <span class="token comment">//1 获取所有 public 方法,包括父类的</span>
    <span class="token class-name">Method</span><span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Method</span> m <span class="token operator">:</span> methods<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//2 获取所有方法,包括 private 不包括父类的</span>
    <span class="token class-name">Method</span><span class="token punctuation">[</span><span class="token punctuation">]</span> methods2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Method</span> m <span class="token operator">:</span> methods2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//3 获取指定参数的 public 的方法,包括父类的</span>
    <span class="token class-name">Method</span> sayHelloMethod <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">&quot;sayHello&quot;</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>sayHelloMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//4 获取指定参数的private 方法,不包括父类的</span>
    <span class="token class-name">Method</span> doWorkMethod <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">&quot;doWork&quot;</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>doWorkMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h4 id="_2-4-2-调用方法"><a href="#_2-4-2-调用方法" class="header-anchor">#</a> 2.4.2 调用方法</h4> <p><strong>API</strong> :</p> <p>public Object invoke(Object obj, Object... args):
obj: 表示调用该方法要作用到那个对象上.
args:调用方法的实际参数
方法的返回值表示,调用该方法是否有返回值,如果有就返回,如果没有,返回null.</p> <p><strong>传统创建对象调用方法</strong>:
Person p = new Person(1L, &quot;小狼&quot;);
p.sleep(5);// 小狼,睡,5个时辰.</p> <p><strong>使用反射创建对象调用方法</strong>:
Method m = clz.getMethod(“sleep”, int.class);// 找到sleep方法.
m.invoke(obj, 5);// 睡,小狼,5个时辰.</p> <p>实战:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testGetMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1 获取字节码对象</span>
    <span class="token class-name">Class</span> clz <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode._01_reflect.Person&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 获取构造器来创建对象</span>
    <span class="token class-name">Object</span> obj <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 使用公共的无参数的构造器</span>
    <span class="token comment">// 3 获取方法</span>
    <span class="token comment">//1 获取所有 public 方法,包括父类的</span>
    <span class="token class-name">Method</span><span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Method</span> m <span class="token operator">:</span> methods<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//2 获取所有方法,包括 private 不包括父类的</span>
    <span class="token class-name">Method</span><span class="token punctuation">[</span><span class="token punctuation">]</span> methods2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Method</span> m <span class="token operator">:</span> methods2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//3 获取指定参数的 public 的方法,包括父类的</span>
    <span class="token class-name">Method</span> sayHelloMethod <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">&quot;sayHello&quot;</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>sayHelloMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用方法</span>
    <span class="token class-name">Object</span> val1 <span class="token operator">=</span> sayHelloMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">&quot;小狼&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;值1:&quot;</span> <span class="token operator">+</span> val1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//4 获取指定参数的private 方法,不包括父类的</span>
    <span class="token class-name">Method</span> doWorkMethod <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">&quot;doWork&quot;</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>doWorkMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置可访问</span>
    doWorkMethod<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用私有的方法</span>
    doWorkMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token string">&quot;小马&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p><strong>注意</strong>:</p> <ol><li>方法也是可以被访问私有修饰符修饰的,所以,如果要访问非 public 修饰的方法,需要在访问之前设置可访问
method.setAccessible(true);</li> <li>如果调用的是静态方法,是不需要对象的,所以此时在 invoke 方法的第一个参数,对象直接传递一个null 即可.</li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token comment">// 调用静态方法</span>
<span class="token class-name">Method</span> staticSayHelloMethod <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span><span class="token string">&quot;sayHello&quot;</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 不需要对象去调用,但是参数必须加上null,不然会把后面的参数作为调用方法的对象了.</span>
staticSayHelloMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token string">&quot;小明&quot;</span><span class="token punctuation">,</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>Person.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span></span>_01_reflect<span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">,</span> <span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">private</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string">&quot;: hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;您好&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span><span class="token class-name">Long</span> id<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;调用静态方法&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;doWork&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string">&quot;: doWork&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// getter方法 setter 方法</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;Person{&quot;</span> <span class="token operator">+</span>
                <span class="token string">&quot;id=&quot;</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                <span class="token string">&quot;, name='&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token char">'\''</span> <span class="token operator">+</span>
                <span class="token char">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="_2-5-操作字段-了解"><a href="#_2-5-操作字段-了解" class="header-anchor">#</a> 2.5 操作字段(了解)</h3> <p>在反射中, 使用 Field  去描述字段这一类事物.</p> <h4 id="_2-5-1-获取字段"><a href="#_2-5-1-获取字段" class="header-anchor">#</a> 2.5.1 获取字段</h4> <p><strong>获取单个字段</strong>:</p> <p>public Field getField(String name)  : name 要获取的字段的名称
public Field getDeclaredField(String name) :</p> <p><strong>获取所有字段</strong></p> <p>public Field[] getFields()
public Field[] getDeclaredFields()</p> <p>代码:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testField</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1 获取字节码对象</span>
    <span class="token class-name">Class</span> clz <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span>
    <span class="token class-name">Object</span> obj <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 获取字段</span>
    <span class="token class-name">Field</span><span class="token punctuation">[</span><span class="token punctuation">]</span> fs <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Field</span> f<span class="token operator">:</span> fs<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Field</span><span class="token punctuation">[</span><span class="token punctuation">]</span> fs2 <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Field</span> f<span class="token operator">:</span> fs2<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 获取单个字段</span>
    <span class="token class-name">Field</span> nameField <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">getDeclaredField</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nameField<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h4 id="_2-5-2-操作字段"><a href="#_2-5-2-操作字段" class="header-anchor">#</a> 2.5.2 操作字段</h4> <p>get(Object obj);</p> <p>set(Object obj,Object value)</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 设置私有字段可访问</span>
nameField<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 操作name字段</span>
<span class="token comment">// 设置那么字段的数据</span>
nameField<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token string">&quot;小狼&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 获取name字段的数据</span>
<span class="token class-name">Object</span> nameValue <span class="token operator">=</span> nameField<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nameValue<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="三、javabean"><a href="#三、javabean" class="header-anchor">#</a> 三、JavaBean</h2> <h3 id="_3-1-javabean-介绍"><a href="#_3-1-javabean-介绍" class="header-anchor">#</a> 3.1 JavaBean 介绍</h3> <p>Javabean 是 Java 中最重要的一个可重用的<strong>组件</strong>(减少代码重复,可重用,封装业务逻辑,封装数据).</p> <p><strong>组件</strong>:一些符合某种规范的类,可以完成特定的功能.</p> <p><strong>JavaBean 的规范要求</strong>:</p> <ol><li>使用 public 修饰.</li> <li>字段私有化.</li> <li>提供 get/set 方法.</li> <li>公共的无参数的构造器.(使用反射,使用字节码对象.newInstance去创建对象.)</li></ol> <p><strong>三大成员</strong>:</p> <ol><li>事件.</li> <li>方法.</li> <li><strong>属性</strong>(最重要的一个概念).</li></ol> <p><strong>什么是属性( property )呢?</strong>:</p> <p>JavaBean 可以封装数据,就是将数据保存到一个 bean 对象的属性中的.<strong>属性不是字段,属性是通过 get/set 方法推导出来的.</strong></p> <p><strong>规范的get方法/获取方法/读方法</strong>:  public 修饰,无参数,有返回,get开头.
<strong>规范的set方法/设置方法/写方法</strong>:  public 修饰,有参数,无返回,set开头.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426132457453.png" alt="image-20200426132457453"></p> <p><strong>注意</strong>:</p> <ol><li>只要是标准的get/set方法,就存在属性.不一定非得是通过工具自动生成的规范的写法.</li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426132553855.png" alt="image-20200426132553855"></p> <p>以上代码,并不存在字段 startIndex, 但是因为 getStartIndex 方法是规范的 get 方法,所以就存在属性 startIndex.</p> <ol start="2"><li>字段是 boolean 的,读方法不是 get 开头,是 is 开头.</li></ol> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426132700606.png" alt="image-20200426132700606"></p> <h3 id="_3-2-lombok-使用"><a href="#_3-2-lombok-使用" class="header-anchor">#</a> 3.2 LomBok 使用</h3> <p>lombok 是开源的代码生成库，是一款非常实用的小工具，我们在编辑实体类时可以通过 lombok 注解减少</p> <p>getter、setter等方法的编写，在更改实体类时只需要修改属性即可，减少了很多重复代码的编写工作。</p> <h4 id="_3-2-1-lombok-插件安装"><a href="#_3-2-1-lombok-插件安装" class="header-anchor">#</a> 3.2.1 Lombok 插件安装</h4> <ol><li>点击菜单栏中的 File--&gt;Settings，或者使用快捷键 Ctrl+Alt+S进入到设置页面。</li> <li>Plugins -&gt; 选择 Browse repositories -&gt; 搜索页面输入lombok-&gt;点击 Install 按钮</li> <li>Settings 设置页面-&gt; Build，Execution，Deployment--&gt;选择Compiler--&gt;选中Annotation Processors，然后在右侧勾选 Enable annotation processing即可。</li> <li>New Projects Settings设置页面-&gt;Build，Execution，Deployment--&gt;选择Compiler--&gt;选中Annotation Processors，然后在右侧勾选 Enable annotation processing即可。</li> <li>重启工具</li></ol> <h4 id="_3-2-2-lombok-工具包导入"><a href="#_3-2-2-lombok-工具包导入" class="header-anchor">#</a> 3.2.2 Lombok 工具包导入</h4> <ol><li>下载拷贝 lombok.1.18.20 包到项目中</li> <li>引用 lombok 包</li></ol> <h4 id="_3-2-3-lombok-的使用-掌握"><a href="#_3-2-3-lombok-的使用-掌握" class="header-anchor">#</a> 3.2.3 Lombok 的使用(掌握)</h4> <ol><li>@Getter : 生成 getter 方法</li> <li>@Setter : 生成 setter 方法</li> <li>@Data : 生成类中所有支持的方法(不推荐)</li> <li>@ToString: 生成 toString 方法</li> <li>@NoArgsConstructor : 无参构造器</li> <li>@AllArgsConstructor : 全参构造器</li></ol> <h2 id="四、内省"><a href="#四、内省" class="header-anchor">#</a> 四、内省</h2> <h3 id="_4-1-内省介绍"><a href="#_4-1-内省介绍" class="header-anchor">#</a> 4.1 内省介绍</h3> <p>JavaBean是一个非常常用的组件,无外乎就是操作里面的属性.而之前咱们要获取JavaBean中的方法,如果使用反射非常麻烦.SUN公司专门提供了一套操作 Javabean 属性的API: <strong>内省</strong>.</p> <p><strong>目标</strong>:记住内省的核心类 <strong>Introspector</strong>,熟练操作 JavaBean 的属性.</p> <p><strong>内省的入口</strong>: Introspector</p> <h3 id="_4-2-内省的作用"><a href="#_4-2-内省的作用" class="header-anchor">#</a> 4.2 内省的作用:</h3> <ol><li>获取到属性名和属性类型等相关状态信息.</li> <li>获取属性对应的读写方法操作属性的值等操作方式.</li></ol> <h3 id="_4-3-内省常用的api"><a href="#_4-3-内省常用的api" class="header-anchor">#</a> 4.3 内省常用的API</h3> <ol><li>通过字节码对象,获取到 Javabean 的描述对象.
Introspector类:
public static BeanInfo getBeanInfo(Class beanClass, Class stopClass):返回 Javabean的描述对象</li> <li>通过 Javabean 描述对象获取属性描述器.
BeanInfo类: PropertyDescriptor[] getPropertyDescriptors(): 获取属性描述器.</li> <li>通过属性描述器,获取到属性名,属性类型,读写方法.
PropertyDescriptor类:
public String getName(): 获取属性名
public Class getPropertyType(): 获取属性类型
public Method getReadMethod():获取读方法
public Method getWriteMethod():获取写方法</li></ol> <h3 id="_4-4-实战"><a href="#_4-4-实战" class="header-anchor">#</a> 4.4 实战</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testIntrospector</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建对象来调用方法</span>
    <span class="token class-name">Person</span> obj <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 把 JavaBean 转成 beanInfo</span>
    <span class="token class-name">BeanInfo</span> beanInfo <span class="token operator">=</span> <span class="token class-name">Introspector</span><span class="token punctuation">.</span><span class="token function">getBeanInfo</span><span class="token punctuation">(</span><span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2 通过beanInfo 获取所有的属性</span>
    <span class="token class-name">PropertyDescriptor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> pds <span class="token operator">=</span> beanInfo<span class="token punctuation">.</span><span class="token function">getPropertyDescriptors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 3 遍历属性描述器数组,获取到每个属性描述器</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">PropertyDescriptor</span> pd<span class="token operator">:</span> pds<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获取属性名</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;属性名:&quot;</span> <span class="token operator">+</span> pd<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 获取属性类型</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;属性类型:&quot;</span> <span class="token operator">+</span> pd<span class="token punctuation">.</span><span class="token function">getPropertyType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 获取属性的getter 和setter 方法</span>
        <span class="token class-name">Method</span> getMethod <span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getReadMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Method</span> setMethod <span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getWriteMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;属性getter:&quot;</span> <span class="token operator">+</span> getMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;属性setter:&quot;</span> <span class="token operator">+</span> setMethod<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 调用属性的 getter 和 setter方法</span>
        <span class="token comment">// 调用name属性的setter 方法</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>pd<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            setMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">&quot;小狼&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 调用所有属性的getter 方法</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>getMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>通过字节码对象来获取 BeanInfo 对象的时候,默认会内省当前字节码对象以及其所有的父类的信息.比如,getBeanInfo(A.class),其实它也会内省A的父类,如Object的信息.一般来说,我们不关心父类的属性相关信息,此时可以调用getBeanInfo的重载方法.getBeanInfo(beanClass,stopClass)</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426155439798.png" alt="image-20200426155439798"></p> <p><strong>实战</strong>:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">BeanInfo</span> beanInfo <span class="token operator">=</span> <span class="token class-name">Introspector</span><span class="token punctuation">.</span><span class="token function">getBeanInfo</span><span class="token punctuation">(</span><span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-5-javabean-和-map-之间的转化"><a href="#_4-5-javabean-和-map-之间的转化" class="header-anchor">#</a> 4.5 JavaBean 和 Map 之间的转化</h3> <p><strong>目标</strong>: 掌握  JavaBean 和 map 的结构,通过分析结构的相似,锻炼需求分解的能力.</p> <p><strong>1. 为什么需要将 JavaBean 和 Map 进行转换?</strong>
在很多应用场景中,需要将 key=value 形式的数据( ResultSet )与 JavaBean 对象相互转换.</p> <p><strong>2. 为什么具备可以转换的条件?</strong></p> <p>并不是说,任何两种结构的数据都是可以相互转换的.之所以map和JavaBean可以转换,是因为它们在数据结构上就极其相似.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20200426134058204.png" alt="image-20200426134058204"></p> <p>所以,我们可以将 map 和 JavaBean 相互转换.将 key 和属性名一一对应起来.</p> <p><strong>实战</strong>:</p> <p>Javabean 转 map</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Javabean 转 map</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token function">bean2map</span><span class="token punctuation">(</span><span class="token class-name">Object</span> obj<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建要给Map 对象</span>
    <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 1 把 obj 通过内省去得到所有的属性</span>
    <span class="token class-name">BeanInfo</span> beanInfo <span class="token operator">=</span> <span class="token class-name">Introspector</span><span class="token punctuation">.</span><span class="token function">getBeanInfo</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">PropertyDescriptor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> pds <span class="token operator">=</span> beanInfo<span class="token punctuation">.</span><span class="token function">getPropertyDescriptors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">PropertyDescriptor</span> pd <span class="token operator">:</span> pds<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 2 获取属性名作为map 的key</span>
        <span class="token class-name">String</span> key <span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 3 获取属性的getter方法并调用,得到属性值作为map 的value</span>
        <span class="token class-name">Object</span> value<span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getReadMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
        map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span>  map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>测试方法:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testBeanToMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token class-name">Person</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">,</span><span class="token string">&quot;小码&quot;</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token class-name">BeanToMapUtil</span><span class="token punctuation">.</span><span class="token function">bean2map</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>map转javabean</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// map 转为 JavaBean</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Object</span> <span class="token function">map2bean</span><span class="token punctuation">(</span><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map<span class="token punctuation">,</span> <span class="token class-name">Class</span> clz<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建JavaBean对象</span>
    <span class="token class-name">Object</span> obj <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历属性,获取属性名作为map 的key 去获取value值,再设置给setter 方法</span>
    <span class="token comment">// 1 获取所有的属性</span>
    <span class="token class-name">BeanInfo</span> beanInfo <span class="token operator">=</span> <span class="token class-name">Introspector</span><span class="token punctuation">.</span><span class="token function">getBeanInfo</span><span class="token punctuation">(</span>clz<span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">PropertyDescriptor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> pds <span class="token operator">=</span> beanInfo<span class="token punctuation">.</span><span class="token function">getPropertyDescriptors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">PropertyDescriptor</span> pd <span class="token operator">:</span> pds<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 2 获取属性名作为map 的key 去获取属性值</span>
        <span class="token class-name">String</span> key <span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 3 获取到属性值,调用属性的setter方法去设置属性值</span>
        <span class="token class-name">Object</span> value <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pd<span class="token punctuation">.</span><span class="token function">getWriteMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> obj<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>测试代码:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testBeanToMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">Person</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">,</span><span class="token string">&quot;小码&quot;</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token class-name">BeanToMapUtil</span><span class="token punctuation">.</span><span class="token function">bean2map</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 测试 map2bean</span>
        <span class="token class-name">Person</span> p2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Person</span><span class="token punctuation">)</span><span class="token class-name">BeanToMapUtil</span><span class="token punctuation">.</span><span class="token function">map2bean</span><span class="token punctuation">(</span>map<span class="token punctuation">,</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>p2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>问题: 调用者已经告诉工具方法要把map转为 Person,而拿到数据之后任然需要做强转,不合理.</p> <p>优化方式: 使用泛型 (了解,可不用写)</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// map 转为 JavaBean</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token class-name">T</span> <span class="token function">map2bean</span><span class="token punctuation">(</span><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map<span class="token punctuation">,</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> clz<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建JavaBean对象</span>
    <span class="token class-name">T</span> obj <span class="token operator">=</span> clz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历属性,获取属性名作为map 的key 去获取value值,再设置给setter 方法</span>
    <span class="token comment">// 1 获取所有的属性</span>
    <span class="token class-name">BeanInfo</span> beanInfo <span class="token operator">=</span> <span class="token class-name">Introspector</span><span class="token punctuation">.</span><span class="token function">getBeanInfo</span><span class="token punctuation">(</span>clz<span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">PropertyDescriptor</span><span class="token punctuation">[</span><span class="token punctuation">]</span> pds <span class="token operator">=</span> beanInfo<span class="token punctuation">.</span><span class="token function">getPropertyDescriptors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">PropertyDescriptor</span> pd <span class="token operator">:</span> pds<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 2 获取属性名作为map 的key 去获取属性值</span>
        <span class="token class-name">String</span> key <span class="token operator">=</span> pd<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 3 获取到属性值,调用属性的setter方法去设置属性值</span>
        <span class="token class-name">Object</span> value <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
        pd<span class="token punctuation">.</span><span class="token function">getWriteMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> obj<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>测试:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">testBeanToMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">Person</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">,</span><span class="token string">&quot;小码&quot;</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token class-name">BeanToMapUtil</span><span class="token punctuation">.</span><span class="token function">bean2map</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Person</span> p2 <span class="token operator">=</span> <span class="token class-name">BeanToMapUtil</span><span class="token punctuation">.</span><span class="token function">map2bean</span><span class="token punctuation">(</span>map<span class="token punctuation">,</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>p2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_4-6-小结"><a href="#_4-6-小结" class="header-anchor">#</a> 4.6 小结</h3> <ol><li>理解和掌握 JavaBean 的规范</li> <li>理解和掌握什么是 JavaBean 属性 (写 JavaBean)</li> <li>理解内省的作用</li> <li>掌握使用内省去查看和操作 JavaBean 属性</li> <li>了解 JavaBean 和 Map 的转换(写下代码,感受下就OK)</li></ol> <p>拓展:不做要求,有时间有精力做</p> <ol><li>使用内省去完成 打印任何 JavaBean 的数据. (拓展)</li> <li>使用内省去实现  JDBC 中结果集的统一处理(使用工具方法去处理)</li></ol> <h2 id="五、小结与作业"><a href="#五、小结与作业" class="header-anchor">#</a> 五、小结与作业</h2> <h3 id="_5-1-小结"><a href="#_5-1-小结" class="header-anchor">#</a> 5.1 小结</h3> <ul><li><p>理解为什么需要使用反射</p></li> <li><p>掌握字节码对象含义</p></li> <li><p>掌握获取字节码对象的方式(总共有3种方式)</p></li> <li><p>掌握通过字节码对象去获取构造器以及调用构造器</p></li> <li><p>掌握方法的获取和调用</p></li> <li><p>了解字段的操作</p></li> <li><p>掌握JavaBean 中属性的含义</p></li> <li><p>使用Lombok 来给JavaBean 生成Getter Setter 和构造器以及toString 方法</p></li></ul> <h3 id="_5-2-作业"><a href="#_5-2-作业" class="header-anchor">#</a> 5.2 作业</h3> <ul><li><p>理解反射的作用</p></li> <li><p>掌握通过反射来获取字节码对象</p></li> <li><p>通过反射来获取和操作构造器</p></li> <li><p>通过反射来获取和操作方法</p></li> <li><p>使用Lombok 来生成JavaBean 需要的方法</p></li> <li><p>使用反射去完成 工具方法来打印 JavaBean 中的数据</p></li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=%E5%8F%8D%E5%B0%84" title="标签">#反射</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/258730/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">单元测试与配置文件</div></a> <a href="/pages/80d6e3/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Lambda表达式</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/258730/" class="prev">单元测试与配置文件</a></span> <span class="next"><a href="/pages/80d6e3/">Lambda表达式</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/29.088e0d73.js" defer></script>
  </body>
</html>
